var Attention = Class.create();
Object.extend(Attention, {
	add: function(uid){
		new Ajax.Request("/~attention.index.add",{
			method: 'post',
			parameters: 'uid='+uid,
			onComplete: this.onAddComplete.bindAsEventListener(this)
		});
	},
	confirmRemove: function(uid){
		new MessageBox(MessageBox.TYPE_YES_NO, '继续操作将取消对此用户的关注', '取消关注', {yes: this.remove.bind(this, uid)});
	},
	remove: function(uid){
		
		new Ajax.Request("/~attention.index.remove",{
			method: 'post',
			parameters: 'uid='+uid,
			onComplete: this.onRemoveComplete.bindAsEventListener(this)
		});
		
	},
	prompt: function(){
		new MessageBox(MessageBox.TYPE_PROMPT, '输入对方的uid或用户名', '添加关注', {ok: this.addRequest});
	},
	addRequest: function(uid, messagebox){
		new Ajax.Request("/~attention.index.add",{
			method: 'post',
			parameters: 'add_uid='+uid,
			onComplete: function(transport){
				enable_form();
				var ret = (transport.responseText).evalJSON();
				if(ret.result){
					messagebox.close();
					window.location.reload();
				}else{
					messagebox.showError(__lang[ret.reason]?__lang[ret.reason]:ret.descriptions);
				}
			}
		});
		disable_form();
	},
	onAddComplete: function(transport){
		var ret = (transport.responseText).evalJSON();
		if(ret.result == true){
			new MessageBox(MessageBox.TYPE_MESSAGE, '他已被成功加入您的关注列表！', '成功', {close: function(){window.location.reload();}});
		}else{
			if(ret.reason == -112){
				new MessageBox(MessageBox.TYPE_MESSAGE, '您已经关注过他了', '失败', {});
			}else if(ret.reason == -106){
				new MessageBox(MessageBox.TYPE_MESSAGE, '您关注的用户不存在', '失败', {});
			}else if(ret.reason == -107){
				new MessageBox(MessageBox.TYPE_MESSAGE, '您不能添加您自己为关注对象', '失败', {});
			}else new MessageBox(MessageBox.TYPE_MESSAGE, '未知错误，添加关注失败', '失败', {});
		}
	},
	onRemoveComplete: function(transport){
		var ret = (transport.responseText).evalJSON();
		if(ret.result){
			new MessageBox(MessageBox.TYPE_MESSAGE, '该用户已从您的关注列表中移除！', '提示', {close: function(){window.location.reload();}});
//			location.href = url_add(location.href, {_remove_attention: 1});
		}else{
			new MessageBox(MessageBox.TYPE_MESSAGE,ret.reason, '失败', {});
		}
	}
});

/**
 *  漫画订阅相关操作
 */
var Dig = Class.create();
Object.extend(Dig, {
	dig: function(ctid){
		new Ajax.Request("/~cartoon.log.addDig",{
			method: 'post',
			parameters: 'cid='+ctid,
			onComplete: this.onAddComplete.bindAsEventListener(this)
		});
	},
	confirmRemove: function(uid){
		new MessageBox(MessageBox.TYPE_YES_NO, '继续操作将取消该订阅', '取消订阅', {yes: this.remove.bind(this, uid)});
	},
	remove: function(ctid){		
		new Ajax.Request("/~cartoon.subscribe.cancel",{
			method: 'post',
			parameters: 'ctid='+ctid,
			onComplete: this.onRemoveComplete.bindAsEventListener(this)
		});
		
	},
	prompt: function(){
		new MessageBox(MessageBox.TYPE_PROMPT, '输入对方的uid或用户名', '添加关注', {ok: this.addRequest});
	},
	addRequest: function(uid, messagebox){
		new Ajax.Request("/~attention.index.add",{
			method: 'post',
			parameters: 'add_uid='+uid,
			onComplete: function(transport){
				enable_form();
				var ret = (transport.responseText).evalJSON();
				if(ret.result){
					messagebox.close();
					window.location.reload();
				}else{
					messagebox.showError(__lang[ret.reason]?__lang[ret.reason]:ret.descriptions);
				}
			}
		});
		disable_form();
	},
	onAddComplete: function(transport){
		var ret = (transport.responseText).evalJSON();
		if(ret.result){
//			new MessageBox(MessageBox.TYPE_MESSAGE, '操作成功', '成功', {});
			var dignum = parseInt($('dignum').innerHTML);
			$('dignum').innerHTML = dignum + 1;
		}else{
			new MessageBox(MessageBox.TYPE_MESSAGE, ret.reason, '失败', {});
		}
	},
	onRemoveComplete: function(transport){
		var ret = (transport.responseText).evalJSON();
		if(ret.result){
			new MessageBox(MessageBox.TYPE_MESSAGE, '取消订阅成功！', '提示', {close: function(){window.location.reload();}});
		}else{
			new MessageBox(MessageBox.TYPE_MESSAGE,ret.reason, '失败', {});
		}
	}
});

/**
 *  漫画订阅相关操作
 */
var Subscribe = Class.create();
Object.extend(Subscribe, {
	add: function(ctid){
		new Ajax.Request("/~cartoon.subscribe.add",{
			method: 'post',
			parameters: 'cid='+ctid,
			onComplete: this.onAddComplete.bindAsEventListener(this)
		});
	},
	confirmRemove: function(uid){
		new MessageBox(MessageBox.TYPE_YES_NO, '继续操作将取消该订阅', '取消订阅', {yes: this.remove.bind(this, uid)});
	},
	remove: function(ctid){		
		new Ajax.Request("/~cartoon.subscribe.cancel",{
			method: 'post',
			parameters: 'ctid='+ctid,
			onComplete: this.onRemoveComplete.bindAsEventListener(this)
		});
		
	},
	prompt: function(){
		new MessageBox(MessageBox.TYPE_PROMPT, '输入对方的uid或用户名', '添加关注', {ok: this.addRequest});
	},
	addRequest: function(uid, messagebox){
		new Ajax.Request("/~attention.index.add",{
			method: 'post',
			parameters: 'add_uid='+uid,
			onComplete: function(transport){
				enable_form();
				var ret = (transport.responseText).evalJSON();
				if(ret.result){
					messagebox.close();
					window.location.reload();
				}else{
					messagebox.showError(__lang[ret.reason]?__lang[ret.reason]:ret.descriptions);
				}
			}
		});
		disable_form();
	},
	onAddComplete: function(transport){
		var ret = (transport.responseText).evalJSON();
		if(ret.result){
			new MessageBox(MessageBox.TYPE_MESSAGE, '已经订阅成功', '成功', {close: function(){window.location.reload();}});
		}else{
//			new MessageBox(MessageBox.TYPE_MESSAGE, ret.reason, '失败', {});
			if(ret.reason == -112){
				new MessageBox(MessageBox.TYPE_MESSAGE, '您已经订阅过了！', '失败', {});
			}
			else{
				new MessageBox(MessageBox.TYPE_MESSAGE, '订阅失败，请稍候再试！', '失败', {});
			}
		}
	},
	onRemoveComplete: function(transport){
		var ret = (transport.responseText).evalJSON();
		if(ret.result){
			new MessageBox(MessageBox.TYPE_MESSAGE, '取消订阅成功！', '提示', {close: function(){window.location.reload();}});
		}else{
			new MessageBox(MessageBox.TYPE_MESSAGE,ret.reason, '失败', {});
		}
	}
});

/**
 *  漫画订阅相关操作
 */
var Collect = Class.create();
Object.extend(Collect, {
	add: function(ctid){
		var cid = 0;
		if(arguments.length>=2){
			cid	=	arguments[1];
		}
		new Ajax.Request("/~cartoon.collect.add",{
			method: 'post',
			parameters: $H({ctid:ctid,cid:cid}),
			onComplete: this.onAddComplete.bindAsEventListener(this)
		});
	},
	confirmRemove: function(id){
		new MessageBox(MessageBox.TYPE_YES_NO, '继续操作将删除收藏', '删除收藏', {yes: this.remove.bind(this, uid)});
	},
	remove: function(id){		
		new Ajax.Request("/~cartoon.collect.remove",{
			method: 'post',
			parameters: 'id='+id,
			onComplete: this.onRemoveComplete.bindAsEventListener(this)
		});
		
	},
	onAddComplete: function(transport){
		var ret = (transport.responseText).evalJSON();
		if(ret.result){
			new MessageBox(MessageBox.TYPE_MESSAGE, '已经收藏成功', '成功', {close: function(){window.location.reload();}});
		}else{
			new MessageBox(MessageBox.TYPE_MESSAGE, ret.reason, '失败', {});
		}
	},
	onRemoveComplete: function(transport){
		var ret = (transport.responseText).evalJSON();
		if(ret.result){
			new MessageBox(MessageBox.TYPE_MESSAGE, '删除收藏成功！', '提示', {close: function(){window.location.reload();}});
		}else{
			new MessageBox(MessageBox.TYPE_MESSAGE,ret.reason, '失败', {});
		}
	}
});